<template>
	<uv-navbar :fixed="true" :placeholder="true" :title="title" left-arrow @leftClick="$onClickLeft" />
	<view class="container">
		<!-- 	<view class="bg-white">
			<uv-tabs :list="tabList" :current="current" @change="change" keyName="name" :scrollable="false"></uv-tabs>
		</view> -->
		<view class="orders-list d-flex flex-column w-100" style="padding: 20rpx; padding-bottom: 0;">
			<view class="order-item" v-for="(item, index) in orders" :key="index" style="margin-bottom: 30rpx;"
				@click="detail(item.id)">
				<list-cell :hover="false">
					<view class="w-100 d-flex align-items-center">
						<view class="flex-fill d-flex flex-column">
							<!-- <view class="font-size-lg text-color-base" style="margin-bottom: 20rpx;">
								{{ item.shop.name }}
							</view> -->
							<view class="font-size-sm text-color-assist">充电订单ID：{{ item.orderId }}</view>
							<view class="font-size-sm text-color-assist">站点ID：{{ item.stationId }}</view>
							<view class="font-size-sm text-color-assist">站点名：{{ item.stationName }}</view>
							<view class="font-size-sm text-color-assist">创建时间：{{ item.createdTime }}</view>

						</view>

					</view>
				</list-cell>
				<list-cell :hover="false" last>
					<view class="w-100 d-flex flex-column">
					<!-- 	<view class="w-100 text-truncate font-size-lg text-color-base" style="margin-bottom: 20rpx;">
									<view class="flex mb-2" v-for="(good,index) in item.cartInfo" :key="index">
								<image :src="good.image" mode="aspectFill" class="image"></image>
								<view class="flex flex-column">
									<view class="font-size-medium mt-1 text-color-base">{{ good.title }}</view>
									<view class="font-size-sm mt-1">{{ good.spec }}</view>
									<view class="font-size-sm mt-2">×{{ good.number }} ¥{{ good.price }}</view>
								</view>
							</view>
						</view> -->
						<!-- <view class="d-flex justify-content-between align-items-center" style="margin-bottom: 30rpx;">
					
						</view> -->
						<view class="d-flex align-items-center justify-content-end">
							<view>
								<button class="left-margin" plain size="mini" @tap.stop="detail(item.id)">订单详情</button>
							</view>
						</view>
					</view>
				</list-cell>
			</view>
		</view>
		<uv-empty v-if="orders.length == 0" mode="order"></uv-empty>
	</view>
</template>


<script setup>
	import {
		ref,
		computed
	} from 'vue'
	import {
		useMainStore
	} from '@/store/store'
	import {
		storeToRefs
	} from 'pinia'
	import {
		onLoad,
		onPullDownRefresh,
		onReachBottom
	} from '@dcloudio/uni-app'
	import {
		formatDateTime,
		kmUnit
	} from '@/utils/util'
	import {
		orderGetOrders,
		orderReceive
	} from '@/api/order'
	const main = useMainStore()
	const {
		isLogin
	} = storeToRefs(main)
	const title = ref('我的订单')

	const pageNum = ref(1)
	const pageSize = ref(10)
	const orders = ref([])
	const tabList = ref([{
		type: -1,
		name: '全部',
	}, {
		type: 0,
		name: '待支付',
	}, {
		type: 1,
		name: '进行中'
	}, {
		type: 4,
		name: '已完成'
	}, {
		type: -3,
		name: '退款单'
	}])
	const current = ref(0)
	const type = ref(-1)

	const goodsNum = computed(() => { //计算单个饮品添加到购物车的数量
		return (goods) => {
			let num = 0;
			goods?.forEach(good => num += parseInt(good.number))
			return num;
		}
	})
	onLoad(() => {
		console.log("load===")
		// if (!isLogin.value) {
		// 	uni.navigateTo({
		// 		url: '/pages/components/pages/login/login'
		// 	})
		// }
		getOrders(false)
	})
	onPullDownRefresh(() => {
		getOrders(false)
	})
	onReachBottom(() => {
		getOrders(false)
	})

	// tab栏切换
	const change = (e) => {
		type.value = e.type
		getOrders(true)
	}

	const getOrders = async (isRefresh = false) => {
		uni.showLoading({
			title: '加载中'
		})
		if (isRefresh) {
			orders.value = []
			pageNum.value = 1
		}
		let ordersData = await orderGetOrders({
			pageNum: pageNum.value,
			pageSize: pageSize.value,
		});
		let data = ordersData.data.data.list
		// console.log("工单的===", ordersData.data.data.list)

		if (data && data.length > 0) {
			orders.value = orders.value.concat(data)
			pageNum.value += 1


		}
		uni.stopPullDownRefresh();
		uni.hideLoading()
	}
	const detail = (id) => {
		uni.navigateTo({
			url: `/pages/components/pages/orders/detail?id=${id}`
		})
	}
	// 确认收到货
	const receive = async (order) => {
		// let data = await orderReceive({
		// 	uni: order.orderId
		// });
		// if (data) {
		// 	await getOrders(true)
		// }
	}
</script>

<style lang="scss" scoped>
	.left-margin {
		margin-left: 10rpx;
	}

	.image {
		width: 160rpx;
		height: 160rpx;
		margin-right: 30rpx;
		border-radius: 8rpx;
	}
</style>